package com.cpi.cornpp.blockdistribution.action;

import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.Globals;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;
import org.apache.struts.util.MessageResources;

import com.cpi.cornpp.blockdistribution.exception.BlockDistributionException;
import com.cpi.cornpp.blockdistribution.form.BlockDistributionForm;
import com.cpi.cornpp.blockdistribution.service.BlockDistributionService;
import com.cpi.cornpp.blockdistribution.view.BlockDistributionView;
import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.monthsperiods.services.MonthsPeriodsService;
import com.cs.common.utils.NumberUtil;

public class BlockDistributionAction extends MappingDispatchAction {
	/** Default forward */
	private final static String DEFAULT_FORWARD = "default";//$NON-NLS-1$
	/** Custom exception */
	private final static String CUSTOM_EXCEPTION = "custom_exception";//$NON-NLS-1$
	/** Id de version */
	private final static String VERSION_ID_LABEL = "versionId";//$NON-NLS-1$
	/** Etiqueta para los periodos mensuales*/
	private final static String MONTHLY_PERIODS_LABEL = "periodosMensuales";//$NON-NLS-1$
	/** Information message name*/
	private final static String MESSAGE = "MESSAGE";//$NON-NLS-1$
	/** Success edit key */
	private static final String SUCCESS_EDIT_LABEL = "common.edit.success";//$NON-NLS-1$
	/** Success delete key */
	private static final String SUCCESS_DELETE_LABEL = "common.delete.success";//$NON-NLS-1$
	/** Id de la orden de compra*/
	private static final String PURCHASE_ORDER_VERSION_ID_LABEL = "purchaseOrderVersionId";//$NON-NLS-1$
	/*
	 * (non-Javadoc)
	 * @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	public ActionForward getBlockDistrubution(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws BlockDistributionException {
		HttpSession httpSession = null;
		BlockDistributionForm blockDistributionForm = null;
		BlockDistributionService blockDistributionService = null;
		MonthsPeriodsService monthsPeriodsService = null;
		BlockDistributionView blockDistributionView = null;
		int versionId = -1;
		int purchaseOrderVersionId = -1;		
		List monthlyPeriods = null;
		try{
			httpSession = request.getSession();
			versionId = NumberUtil.getInt(httpSession.getAttribute(VERSION_ID_LABEL));
			blockDistributionForm = (BlockDistributionForm)form;
			blockDistributionService = new BlockDistributionService();
			monthsPeriodsService = new MonthsPeriodsService();
			
			purchaseOrderVersionId = blockDistributionForm.getBlockDistributionView().getPurchaseOrderVersionId();
			
			blockDistributionView = blockDistributionService.getBlockDistribution(purchaseOrderVersionId);
			blockDistributionView.setPurchaseOrderCode(blockDistributionForm.getBlockDistributionView().getPurchaseOrderCode());
			monthlyPeriods = monthsPeriodsService.listOfMonthsPeriodsForVersion(versionId, purchaseOrderVersionId);
			
			blockDistributionView.setPurchaseOrderVersionId(purchaseOrderVersionId);
			blockDistributionForm.setBlockDistributionView(blockDistributionView);
			request.setAttribute(MONTHLY_PERIODS_LABEL, monthlyPeriods);
		}catch (BlockDistributionException blockDistributionException){
			httpSession.setAttribute(CUSTOM_EXCEPTION, blockDistributionException);
			throw blockDistributionException;
		}catch (Exception exception){
			BlockDistributionException blockDistributionException=null;
			exception.printStackTrace(System.out);
			blockDistributionException= new BlockDistributionException(exception, CornPPException.LAYER_ACTION,CornPPException.ACTION_LISTS);
			httpSession.setAttribute(CUSTOM_EXCEPTION, blockDistributionException);
			throw blockDistributionException;
		}
		return mapping.findForward(DEFAULT_FORWARD);
	}
	/*
	 * (non-Javadoc)
	 * @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	public ActionForward saveBlockDistrubution(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws BlockDistributionException {
		BlockDistributionService blockDistributionService = null;
		HttpSession httpSession = null;
		BlockDistributionForm blockDistributionForm = null;
		MessageResources messageResources = null;
		String messageError = null;
		try{
			httpSession = request.getSession();
			messageResources = getResources(request);
			blockDistributionService = new BlockDistributionService();
			blockDistributionForm = (BlockDistributionForm)form;
			blockDistributionService.saveBlockDistribution(blockDistributionForm.getBlockDistributionView());
			messageError = messageResources.getMessage((Locale)httpSession.getAttribute(Globals.LOCALE_KEY), SUCCESS_EDIT_LABEL);
			httpSession.setAttribute(MESSAGE, messageError);
		}catch (BlockDistributionException blockDistributionException){
			httpSession.setAttribute(CUSTOM_EXCEPTION, blockDistributionException);
			throw blockDistributionException;
		}catch (Exception exception){
			BlockDistributionException blockDistributionException=null;
			exception.printStackTrace(System.out);
			blockDistributionException= new BlockDistributionException(exception, CornPPException.LAYER_ACTION,CornPPException.ACTION_UPDATE);
			httpSession.setAttribute(CUSTOM_EXCEPTION, blockDistributionException);
			throw blockDistributionException;
		}
		return mapping.findForward(DEFAULT_FORWARD);
	}
	/*
	 * (non-Javadoc)
	 * @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	public ActionForward deleteBlockDistrubution(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws BlockDistributionException {
		BlockDistributionService blockDistributionService = null;
		HttpSession httpSession = null;
		MessageResources messageResources = null;
		String messageError = null;
		int purchaseOrderVersionId = -1;
		try{
			httpSession = request.getSession();
			messageResources = getResources(request);
			blockDistributionService = new BlockDistributionService();
			purchaseOrderVersionId = NumberUtil.parseInt(request.getParameter(PURCHASE_ORDER_VERSION_ID_LABEL));
			blockDistributionService.deleteBlockDistribution(purchaseOrderVersionId);
			messageError = messageResources.getMessage((Locale)httpSession.getAttribute(Globals.LOCALE_KEY), SUCCESS_DELETE_LABEL);
			httpSession.setAttribute(MESSAGE, messageError);
		}catch (BlockDistributionException blockDistributionException){
			httpSession.setAttribute(CUSTOM_EXCEPTION, blockDistributionException);
			throw blockDistributionException;
		}catch (Exception exception){
			BlockDistributionException blockDistributionException=null;
			exception.printStackTrace(System.out);
			blockDistributionException= new BlockDistributionException(exception, CornPPException.LAYER_ACTION,CornPPException.ACTION_DELETE);
			httpSession.setAttribute(CUSTOM_EXCEPTION, blockDistributionException);
			throw blockDistributionException;
		}
		return mapping.findForward(DEFAULT_FORWARD);
	}
}